package com.corpdata.app.customer.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.corpdata.app.customer.entity.CustomerInfo;
import com.corpdata.app.customer.service.CustomerInfoService;
import com.corpdata.common.api.pagehelper.PageConvertUtil;
import com.corpdata.common.domain.DataGridRequestDTO;
import com.corpdata.common.result.Result;
import com.corpdata.core.base.BaseController;
import com.corpdata.system.log.WebLog;

/**
 * 客户表
 * 
 * @author wu
 * @email 64088839@qq.com
 * @date 2018-06-13 16:46:46
 */
 
@Controller
@RequestMapping("app/customerInfo")
public class CustomerInfoController extends BaseController{

	@Autowired
	private CustomerInfoService customerInfoService;
	//客户-首页
	@GetMapping("/index")
	public String index(ModelMap map){
		customerInfoService.findCountByState(map);
		return "app/customer/customer_index";
	}
	
	@PostMapping("/sellRecordLog")
	@ResponseBody
	public String sellRecordLog(DataGridRequestDTO dgRequest){
		//根据条件查询所有销售记录 按照时间降序
		return PageConvertUtil.getGridJson(customerInfoService.findsellRecord(dgRequest));
	}
	
	//新增页面
	@GetMapping("/add")
	public String add(){
		return "app/customer/customerInfo_add";
	}
	
	//保存数据
	@WebLog()
	@ResponseBody
	@RequestMapping("/save")
	public Result save(CustomerInfo record){
		return customerInfoService.save(record);
	}
	
	//编辑页面
	@RequestMapping("/edit/{id}")
	public String edit(@PathVariable("id") String id,ModelMap map){
		map.put("record", customerInfoService.findEdit(id));
		return "app/customer/customer_edit";
	}
	
	//更新数据
	@ResponseBody
	@RequestMapping("/update")
	public Result update(CustomerInfo record){
		return customerInfoService.update(record);
	}
	
	//删除
	@WebLog()
	@ResponseBody
	@PostMapping("/delete")
	public Result delete(String id){
		return customerInfoService.deleteById(id);
	}
	
	//批量删除
	@WebLog()
	@ResponseBody
	@PostMapping("/deletes")
	public Result deletes(@RequestParam("ids[]") String[] ids){
		return customerInfoService.deleteByIds(ids);
	}
	
	//详情页面
	@GetMapping("/details/{id}")
	public String details(ModelMap map,@PathVariable("id") String id){
		CustomerInfo ci=customerInfoService.findById(id);
		map.put("record", ci);
		//销售记录+参观记录：销售记录只显示最新可用的
		map.put("sellRecord", customerInfoService.findsellRecordById(ci.getId()));
		//根据客户查询所有销售记录 按照时间降序
		map.put("sellRecordLog", customerInfoService.findsellRecordBycIdAnduId(ci.getId()));
	    return "app/customer/customer_details";
	}
	
	//列表页面
	@GetMapping("/list")
	public String list(){
	    return "app/customer/customer_list";
	}
	//添加客户页面
	@GetMapping("/addClient")
	public String addClient(){
	    return "app/customer/customer_addClient";
	}
	
	//列表数据
	@ResponseBody
	@RequestMapping("/listdata")
	public String findByPage(DataGridRequestDTO dgRequest){
		return customerInfoService.findByPage(dgRequest);
	}
	//参观记录的列表数据
	@ResponseBody
	@RequestMapping("/listVisitData")
	public String findVisitByPage(DataGridRequestDTO dgRequest){
		return customerInfoService.findVisitByPage(dgRequest);
	}
	
	//保存数据
	@ResponseBody
	@RequestMapping("/addCustomer")
	public Result addCustomer(@RequestParam Map<String,String> param){
		return customerInfoService.addCustomer(param);
	}
	//修改数据
	@ResponseBody
	@RequestMapping("/editCustomer")
	public Result editCustomer(@RequestParam Map<String,String> param){
		return customerInfoService.editCustomer(param);
	}
	
	@ResponseBody
	@RequestMapping("/getProjectJson")
	public List<Map<String,String>> getProjectJson() {
		return customerInfoService.getProjectJson();
	}
	
	//更新客户状态  销售记录那些
	@ResponseBody
	@RequestMapping("/updateCustomerStaus")
	public Result updateCustomerStaus(@RequestParam Map<String,String> param){
		return customerInfoService.updateCustomerStaus(param);
	}
	
	//客户首页 根据客户名称或者手机号搜索客户
	@ResponseBody
	@RequestMapping("/findCusByNamePhone")
	public List<Map<String,Object>> findCusByNamePhone(String param){
		return customerInfoService.findCusByNamePhone(param);
	}
	
	//客户首页 根据客户名称或者手机号搜索客户
	@ResponseBody
	@RequestMapping("/findPartitionByProject")
	public List<Map<String,Object>> findPartitionByProject(String projectid){
		return customerInfoService.findPartitionByProject(projectid);
	}
	
	/**
	 * 	根据客户id删除客户何其关联的所有信息
	 * @param cusId
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/delCustomerAll")
	public Result delCustomerAll(String cusId) {
		return customerInfoService.delCustomerAll(cusId);
	}
	
}
